import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: '首页',
        component: () => import('../views/index.vue')
      },
      {
        path: '/userlist',
        name: '用户列表',
        component: () => import('../views/userlist.vue')
      },
      {
        path: '/articlelist',
        name: '文章列表',
        component: () => import('../views/articlelist.vue')
      },
      {
        path: '/orderlist',
        name: '订单列表',
        component: () => import('../views/orderlist.vue')
      },
      {
        path: '/record',
        name: '系统的操作日志',
        component: () => import('../views/record.vue')
      },
      {
        path: '/systemSet',
        name: '系统配置',
        component: () => import('../views/systemSet.vue')
      }
    ]
  },
  {
    path: '/login',
    name: '登录',
    component: () => import('../views/login.vue')
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: 'about' */ '../views/About.vue')
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});
router.beforeEach((to, from, next) => {
  console.log(to, from);
  if (to.path === '/login') {
    next();
  } else if (to.path !== '/login' && localStorage.token) {
    next();
  } else {
    next('/login');
  }
});
export default router;
